package com.jueling.culture.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jueling.culture.core.AbstractService;
import com.jueling.culture.dto.req.QueryVO;
import com.jueling.culture.enums.YesNoEnum;
import com.jueling.culture.mapper.AgentUserMapper;
import com.jueling.culture.mapper.ContributionAwardMapper;
import com.jueling.culture.model.ContributionAward;
import com.jueling.culture.model.CustUser;
import com.jueling.culture.service.ContributionAwardService;
import com.jueling.culture.service.CustUserInfoService;
import com.jueling.culture.util.DateUtil;
import com.jueling.culture.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @describe 贡献奖
 * @author linp
 * @date  2022/04/11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ContributionAwardServiceImpl extends AbstractService<ContributionAward> implements ContributionAwardService {

    @Resource
    @SuppressWarnings("unused")
    private ContributionAwardMapper contributionAwardMapper;
    @Resource
    private AgentUserMapper agentUserMapper;
    @Resource
    private CustUserInfoService custUserInfoService;


    /**
     * 贡献奖领取
     *
     * @param gxjId
     */
    @Override
    public void updateContributionAward(String gxjId) {
        //修改领取状态
        contributionAwardMapper.updateContributionAward(gxjId,"lq");
    }

    /**
     * 贡献奖结算
     *
     */
    @Override
    public void closeContributionAward() {
        //调用存储
        contributionAwardMapper.dealWithContributionAward();
    }

    /**
     * 查询贡献奖列表
     *
     * @param queryVO
     * @return
     */
    @Override
    public PageInfo<ContributionAward> getContributionAwardList(QueryVO queryVO) {
        //判断是否需要级联查询
        if(YesNoEnum.YES.getCode().equals(queryVO.getIsCascade()) && StringUtil.isNotEmptyStr(queryVO.getAgentId())){
            CustUser custUser = custUserInfoService.getCustUserInfoByCustID(queryVO.getAgentId());
            //查询下级代理人
            String custId = agentUserMapper.getLowerCustId(custUser.getCustIdAll());
            //查询下级代理人
            if(StringUtil.isNotEmptyStr(custId)){
                List<String> arrayList = new ArrayList<String>();
                arrayList.addAll(Arrays.asList(custId.split(",")));
                arrayList.add(queryVO.getAgentId());
                queryVO.setAgentIdList(arrayList);
                //保留一条条件
                queryVO.setAgentId(null);
            }
        }
        PageHelper.startPage(queryVO.getPageIndex(), queryVO.getPageSize());
        List<ContributionAward> list = contributionAwardMapper.getContributionAwardList(queryVO);
        PageInfo<ContributionAward> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 贡献奖全量数据
     *
     * @param queryVO
     * @return
     */
    @Override
    public List<List<Object>> getAllContributionAwardList(QueryVO queryVO) {
        //判断是否需要级联查询
        if(YesNoEnum.YES.getCode().equals(queryVO.getIsCascade())){
            //查询下级代理人
            String custId = agentUserMapper.getLowerCustId(queryVO.getAgentId());
            if(StringUtil.isNotEmptyStr(custId)){
                List<String> arrayList = new ArrayList<String>();
                arrayList.addAll(Arrays.asList(custId.split(",")));
                arrayList.add(queryVO.getAgentId());
                queryVO.setAgentIdList(arrayList);
                //保留一条条件
                queryVO.setAgentId(null);
            }
        }
        List<ContributionAward> list = contributionAwardMapper.getContributionAwardList(queryVO);
        return formatData(list);
    }

    /**
     * 数据格式化
     * @param result
     */
    public List<List<Object>> formatData(List<ContributionAward> result){

        List<List<Object>> list = new ArrayList<>();
        int num = 0;
        //格式化数据,并填充数据
        for (ContributionAward contributionAward : result ) {
            num ++;
            List<Object> data = new ArrayList<>();
            //序号
            data.add(num);
            //数据
            data.add(contributionAward.getCustId());
            data.add(contributionAward.getCustNo());
            data.add(contributionAward.getLxdh());
            data.add(DateUtil.dateToString(contributionAward.getDjsj(),DateUtil.FORMAT_DATETIME));
            data.add(contributionAward.getGxj());
            data.add(contributionAward.getGxjzz());
            data.add(contributionAward.getFysfylq() == null ? "-" : contributionAward.getFysfylq() == 1 ? "是" : "否");
            data.add(contributionAward.getJszt() == null ? "-" : contributionAward.getJszt() == 1 ? "是" : "否");
            data.add(contributionAward.getSfxnyh() == null ? "-" : contributionAward.getSfxnyh() == 1 ? "是" : "否");
            data.add(contributionAward.getSjtjrMc());
            data.add(dljbType(contributionAward.getDljbId()));
            data.add(stateType(contributionAward.getYhzt()));
            data.add(contributionAward.getTjrmc());
            data.add(contributionAward.getTjrmcNew());
            data.add(contributionAward.getKhhmc());
            data.add(contributionAward.getKhm());
            data.add(contributionAward.getYhkh());
            data.add(contributionAward.getShdz());
            data.add(DateUtil.dateToString(contributionAward.getJssj(),DateUtil.FORMAT_DATETIME));
            list.add(data);
        }
        return list;
    }

    private static Object stateType(Integer state) {
        if (state == 0) {
            return "待确认";
        }
        if (state == 1) {
            return "已确认";
        }
        if (state == 2) {
            return "停用";
        }
        if (state == 3) {
            return "撤销";
        }
        return null;
    }

    private static String dljbType(String state) {
        if(StringUtil.isEmptyStr(state)){
            return "-";
        }
        if (state.equals("jlwh0001")) {
            return "市级代理";
        }
        if (state.equals("jlwh0002")) {
            return "省级代理";
        }
        if (state.equals("jlwh0003")) {
            return "大区代理";
        }
        if (state.equals("jlwh0004")) {
            return "一星代理";
        }
        if (state.equals("jlwh0005")) {
            return "二星代理";
        }
        if (state.equals("jlwh0006")) {
            return "三星代理";
        }
        if (state.equals("jlwh0007")) {
            return "四星代理";
        }
        if (state.equals("jlwh0008")) {
            return "五星代理";
        }
        return null;
    }
}
